/* Copyright (c) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.orkut.client.api.samples.greetings.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.google.orkut.client.api.samples.greetings.client.DataModel;
import com.google.orkut.client.api.samples.greetings.client.GreetingsCallbackWrapper;
import com.google.orkut.client.api.samples.greetings.client.GreetingsMessages;
import com.google.orkut.client.api.samples.greetings.client.DataModel.DataModelUpdateCallback;
import com.google.orkut.client.api.samples.greetings.client.services.UserInfo;
import com.google.orkut.client.api.samples.greetings.client.services.UserServiceAsync;

/**
 * Shows information about the logged in user and shows a logout link.
 *
 * @author Shishir Birmiwal
 */
public class UserInfoWidget extends Composite implements DataModelUpdateCallback, AsyncCallback<Boolean> {
  private static UserInfoWidgetUiBinder uiBinder = GWT.create(UserInfoWidgetUiBinder.class);
  interface UserInfoWidgetUiBinder extends UiBinder<Widget, UserInfoWidget> {
  }

  @UiField
  Image userThumbnail;

  @UiField
  Label userName;

  @UiField
  Anchor logout;

  private final String postToken;
  private final UserServiceAsync userService;
  private boolean initialized;
  private final DataModel data;
  UserInfo user;

  private final GreetingsMessages msgs;

  private final GreetingsCallbackWrapper cbWrapper;

  @Inject
  public UserInfoWidget(@Named("postToken") String postToken,
      UserServiceAsync userService,
      GreetingsMessages msgs,
      GreetingsCallbackWrapper cbWrapper,
      DataModel data) {
    this.postToken = postToken;
    this.userService = userService;
    this.msgs = msgs;
    this.cbWrapper = cbWrapper;
    this.data = data;
    data.registerCallback(this);
    initWidget(uiBinder.createAndBindUi(this));
  }

  @Override
  protected void onAttach() {
    super.onAttach();
    if (!initialized) {
      userService.getLoggedInUserInfo(postToken,
          cbWrapper.wrap(
              new AsyncCallback<UserInfo>() {
                @Override
                public void onFailure(Throwable caught) {
                  Window.alert(msgs.errorInUserLogin());
                }

                @Override
                public void onSuccess(UserInfo result) {
                  data.updateLoggedInUser(result);
                }
              }));
    }
  }

  @Override
  public void update(DataModel data, Type type) {
    if (!Type.SELECTED_USER.equals(type)) {
      return;
    }
    if (data.selectedUser.equals(user)) {
      return;
    }
    user = data.selectedUser;
    userName.setText(user.userName);
    userThumbnail.setUrl(user.thumbnailUrl);
  }

  @UiHandler("logout")
  void onClick(ClickEvent e) {
    userService.logoutUser(postToken, this);
  }

    // on logout failure
  @Override
  public void onFailure(Throwable arg0) {
    // TODO: show a error message and ask user to try again?
    redirectToHomePage();
  }

    // on logout success
  @Override
  public void onSuccess(Boolean arg0) {
    // TODO: show a error message and ask user to try again on error [arg0==false]
    redirectToHomePage();
  }

  private void redirectToHomePage() {
    Window.Location.replace(Window.Location.getProtocol() + "//" + Window.Location.getHost());
  }
}
